Skip to content

Conversation

@AllieRays
Copy link
Owner

@AllieRays AllieRays commented Oct 25, 2025

Fix JsonPlusRedisSerializer to handle LangChain message serialization

Problem

JsonPlusRedisSerializer.dumps() fails to serialize LangChain message objects (HumanMessage,
AIMessage, etc.), breaking AsyncRedisSaver checkpoint persistence:

TypeError: Type is not JSON serializable: HumanMessage

Solution

  Use orjson's default parameter to delegate LangChain object serialization to the parent class's
  _default handler:

Before:

  def dumps(self, obj: Any) -> bytes:
      return orjson.dumps(obj)  # ❌ Fails for LangChain objects

After:

  def dumps(self, obj: Any) -> bytes:
      return orjson.dumps(obj, default=self._default)  # ✅ Handles all objects

Why This Works

Testing

  • ✅ 8 new test functions covering messages, lists, nested structures
  • ✅ Production verified: AsyncRedisSaver + streaming chat with persistence
  • ✅ Backwards compatible with non-LangChain objects

Related

  - Addresses similar issue to PR redis-developer#108 but with broader scope
  - Supersedes the need for special-casing specific object types
  - Compatible with existing checkpoint/serde infrastructure
 

The parent class JsonPlusSerializer doesn't have dumps() or loads() methods,
only dumps_typed() and loads_typed(). This fix corrects the fallback logic
to properly use the parent's typed serialization methods.

Fixes AttributeError: 'super' object has no attribute 'dumps'
The bug was in line 28 where super().dumps(obj) was called, but the parent
class JsonPlusSerializer doesn't have a dumps() method.

The correct fix is to use orjson.dumps() with default=self._default, which
properly delegates LangChain object serialization to the parent's _default
method.
…dler fix

Tests validate that the fix properly handles:
- HumanMessage, AIMessage, SystemMessage, ToolMessage serialization
- Lists of messages (common LangGraph pattern)
- Nested structures with embedded messages
- dumps_typed() method (used by checkpointer)
- Backwards compatibility with non-LangChain objects
- LangChain serialized format revival

All tests pass, confirming the fix works correctly.
Renamed test_fix_standalone.py to test_jsonplus_redis_serializer.py
to better describe what is being tested (the JsonPlusRedisSerializer class).
@AllieRays AllieRays closed this Oct 27, 2025
@AllieRays AllieRays deleted the fix-jsonplus-serializer-dumps branch October 27, 2025 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants